package com.itboye.jinpuyiyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObservable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.message.NotificationInfoActivity;
import com.itboye.jinpuyiyuan.bean.NoticeBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 通知
 */
public class MainAdapter extends BaseecyclerViewAdapter<MainAdapter.ViewHolder> {
    private List<NoticeBean> mDataList;
    private int mLastPosition = -1;
    private Context context;
    public MainAdapter(Context context) {
        super(context);
        this.context=context;
    }

    public void notifyDataSetChanged(List<NoticeBean> dataList) {
        this.mDataList = dataList;
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = getInflater().inflate(R.layout.item_goodlist, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.doctorName.setText(  mDataList.get(position).getMsg_type_name());
        holder.doctorContent.setText( mDataList.get(position).getContent());
        SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String create_time = mDataList.get(position).getCreate_time();
        long l = Long.parseLong(create_time);
        l= create_time.length()==10?(l*1000):l;

        Date date = new Date(l);
        String format1 = format.format(date);
        holder.date.setText(format1);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NotificationInfoActivity.class);
                intent.putExtra("title",mDataList.get(position).getTitle());
                intent.putExtra("content",mDataList.get(position).getContent());
                context.startActivity(intent);
            }
        });

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView doctorName, doctorContent, date;
        ImageView doctorImg;
        View itemView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            doctorName = (TextView) itemView.findViewById(R.id.doctorName);
            doctorContent = (TextView) itemView.findViewById(R.id.doctorContent);
            date = (TextView) itemView.findViewById(R.id.date);
            doctorImg = (ImageView) itemView.findViewById(R.id.doctorImg);
        }
    }

    private final DataSetObservable mDataSetObservable = new DataSetObservable();

    /**
     * 通知所附观察员，基础数据已更改，任何反映数据集的视图都应该刷新自己。
     */
    public void notifyListDataSetChanged() {
        mDataSetObservable.notifyChanged();
    }

    public MainAdapter refresh(List<NoticeBean> list) {
        mDataList.clear();
        mDataList.addAll(list);
        notifyDataSetChanged();
        notifyListDataSetChanged();
        mLastPosition = -1;
        return this;
    }

    public MainAdapter loadmore(List<NoticeBean> list) {
        mDataList.addAll(list);
        notifyDataSetChanged();
        notifyListDataSetChanged();
        return this;
    }

    //添加一条数据
    public void addData(int position) {
        // mDataList.add(position, "Insert One");
        notifyItemInserted(position);
    }

    //删除一条数据
    public void removeData(int position) {
        mDataList.remove(position);
        notifyItemRemoved(position);
    }

}
